//
//  ViewController.m
//  HYCircleLoadingView
//
//  Created by mac on 15/1/19.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ViewController.h"
#import "CircleLoadingView.h"

@interface ViewController ()

@property (nonatomic,strong) CircleLoadingView *loadingView;
@property (nonatomic,strong) UIButton *startButton;
@property (nonatomic,strong) UIButton *stopButton;
@property (nonatomic,strong) UILabel *_view2;

@property (nonatomic,strong) NSString *startTimeStr;
@property (nonatomic,strong) NSTimer *myTimer;



@end

@implementation ViewController
@synthesize _view2 = view2;
@synthesize myTimer = myTimer;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.loadingView = [[CircleLoadingView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    UIBarButtonItem *loadItem = [[UIBarButtonItem alloc] initWithCustomView:self.loadingView];
    self.navigationItem.leftBarButtonItem = loadItem;
    
    self.startButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.startButton setTitle:@"开始动画" forState:UIControlStateNormal];
    self.startButton.frame = CGRectMake(20, 64, 60, 30);
    [self.startButton addTarget:self
                         action:@selector(startButtonClick:)
               forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.startButton];
    
    self.stopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.stopButton setTitle:@"结束动画" forState:UIControlStateNormal];
    self.stopButton.frame = CGRectMake(20, 94, 60, 30);
    [self.stopButton addTarget:self
                        action:@selector(stopButtonClick:)
              forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.stopButton];
    
    
    
    //scale
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 60, 60)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basic.fromValue = [NSNumber numberWithFloat:0.8f];
    basic.toValue = [NSNumber numberWithInt:1.2f];
    basic.duration = 0.5+arc4random()%10*0.05;
    basic.repeatCount = INT_MAX;
    basic.autoreverses = YES;
    [view1.layer addAnimation:basic forKey:nil];
    
    //bounds
    UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(180, 100, 60, 60)];
    imv.image = [UIImage imageNamed:@"heart"];
    [self.view addSubview:imv];
    
    CABasicAnimation *basicBounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
    basicBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 10, 10)];
    basicBounds.toValue = [NSValue valueWithCGRect:CGRectMake(10, 10, 100, 100)];
    basicBounds.repeatCount = INT_MAX;
    basicBounds.autoreverses = YES;
    basicBounds.duration = 0.5+arc4random()%10*0.05;
    [imv.layer addAnimation:basicBounds forKey:nil];
    
    //corerradios
    view2 = [[UILabel alloc] initWithFrame:CGRectMake(80, 200,260, 50)];
    view2.layer.cornerRadius = 30.0f;
    view2.text = @"网络视乎段卡了哦，请稍后再试";
    view2.textColor = [UIColor whiteColor];
    view2.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view2];
    
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(repeate) userInfo:nil repeats:YES];
    
    [myTimer isValid];
    [self getStartTime];
    
    
    
    

    
    
}


- (NSString *)getStartTime{
    NSDateFormatter *fm = [[NSDateFormatter alloc] init];
    [fm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateTime = [fm stringFromDate:[NSDate date]];
    _startTimeStr = dateTime;
    return _startTimeStr;
}

- (void)repeate{
        [UIView animateWithDuration:1.0f
                         animations:^{
                             view2.alpha = 0.0f;
                         } completion:^(BOOL finished) {
                             view2.alpha = 1.0f;
                         }];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date1 = [formatter dateFromString:_startTimeStr];
    NSDate *date2 = [NSDate date];
    NSTimeInterval aTimer = [date2 timeIntervalSinceDate:date1];
    
    int hour = (int)(aTimer/3600);
    int minute = (int)(aTimer - hour*3600)/60;
    int second = aTimer - hour*3600 - minute*60;
    if (second>3) {
        [myTimer invalidate];
        [view2 removeFromSuperview];
        myTimer = nil;
    }
}

- (void)startButtonClick:(UIButton *)button
{
    [self.loadingView startAnimation];
}

- (void)stopButtonClick:(UIButton *)button
{
    [self.loadingView stopAnimation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
